package ext.tianma.bom.service.Classification;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import ext.tianma.MaterialClassification.CreatePartClassificationNode;
import ext.tianma.MaterialClassification.CreatePartClassificationUtil;
import ext.tianma.MaterialClassification.PartClassificationChange;
import ext.tianma.MaterialClassification.ReadClassification;
import ext.tianma.vProductJurisdiction.VProductJurisdictionProcessor;
import ext.tianma.part.util.countPartAndDocument;

@Controller
@RequestMapping({ "/Classification" })
public class ReadClassifictionSrevice {
	@Autowired
	HttpServletRequest request;
	private String firstValidate = "";

	/**
	 * 读取分类属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/readClassification", method = { RequestMethod.POST, RequestMethod.GET })
	public void doReadClassification(HttpServletResponse response) throws Exception {
		System.out.println("======start=====");
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();

		String result = "";
		// List<String> listClassification = null;
		// 逻辑处理
		try {
			result = ReadClassification.getClassificationNode();
			System.out.println("===》》》" + result);
			out.print(result);
		} catch (Exception e) {
			System.out.println("================" + e);
		}

	}

	/**
	 * 读取分类子节点
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/readClassificationSon", method = { RequestMethod.POST, RequestMethod.GET })
	public void tmReadClassification(@RequestParam(value = "param", required = true) String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// String param222 = request.getParameter("param");
		// 逻辑处理
		// List<String> listClassification = null;
		String result = "";
		// JSONObject obj = new JSONObject(param);
		// String classifiName = obj.getString("");
		result = ReadClassification.getClassificationChildNode(param);

		out.print(result);
	}

	/**
	 * 根据节点名称获取分类属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getClassifictionIba", method = { RequestMethod.POST, RequestMethod.GET })
	public void getClassifictionIba(
			@RequestParam(value = "classificationName", required = true) String classificationName,
			@RequestParam(value = "parentNumber", required = true) String parentNumber, HttpServletResponse response)
			throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String classificationName11 = request.getParameter("classificationName");
		String parentNumber11 = request.getParameter("parentNumber");
		System.out.println(
				"classificationName11===>>" + classificationName11 + "--classificationName--" + classificationName);
		System.out.println("parentNumber11===>>" + parentNumber11 + "--parentNumber--" + parentNumber);
		// System.out.println("======param =========" + param222);

		// 逻辑处理
		String result = "";
		result = ReadClassification.getClassifictionIba(classificationName);

		out.print(result);
	}

	/**
	 * 根据分类获取分类属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/readClassifictionIbaValue", method = { RequestMethod.POST, RequestMethod.GET })
	public void tmReadClassifictionIbaValue(@RequestParam(value = "param", required = true) String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String param222 = request.getParameter("param");
		System.out.println("======param =========" + param222);

		// 逻辑处理
		List<String> listClassification = null;
		JSONObject obj = new JSONObject(param);
		String classifiName = obj.getString("");
		listClassification = ReadClassification.getClassifictionIbaValue(classifiName);

		out.print(listClassification);
	}

	/**
	 * 根据AVL创建物料
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/createPartByAVL", method = { RequestMethod.POST, RequestMethod.GET })
	public void tmCreatePartByAVL(@RequestBody String data, HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String result = "";
		String PartNumber = "";
		// 请求数据已encode 需要decode
		data = URLDecoder.decode(data);
		String AVLPartNumber = "";
		String parentNumber = "";
		String count = "";
		String RFQNumber = "";
		String length = "";
		String width = "";
		String lwRatio = "";
		String materialThickness = "";
		String layerNumber = "";
		String purpose = "";
		String shape = "";
		String defaultUnit = "";
		String supplementaryMsg = "";
		String surfaceCoatingTechnology = "";
		String steelmakingProcess = "";
		String shapeTechnology = "";
		String polishing = "";
		String hole = "";
		String weightValue = "";
		String firstClassification = "";
		System.out.println("===========AVL data:" + data);
		Map<String, String> map = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(data)) {
			JSONObject obj = new JSONObject(data);
			Iterator<String> it = obj.keys();
			while (it.hasNext()) {
				// 获得key
				String ibaName = it.next();
				String iabValue = obj.getString(ibaName);
				map.put(ibaName, iabValue);
			}
		}

		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			String value = map.get(key).toString();
			if (map.get(key) != null) {
				value = map.get(key).toString();
			}
			System.out.println("key==>>" + key + "==value=>>" + value);
			if (key.equals("AVLNumber")) {
				AVLPartNumber = value;
			} else if (key.equals("parentNumber")) {
				parentNumber = value;
			} else if (key.equals("count")) {
				count = value;
			} else if (key.equals("RFQNumber")) {
				RFQNumber = value;
			}else if (key.equals("firstClassification")) {
				firstClassification = value;
			} else if (key.equals("length")) {
				length = value;
			} else if (key.equals("width")) {
				width = value;
			} else if (key.equals("lwRatio")) {
				lwRatio = value;
			} else if (key.equals("materialThickness")) {
				materialThickness = value;
			} else if (key.equals("layerNumber")) {
				layerNumber = value;
			} else if (key.equals("purpose")) {
				purpose = value;
			} else if (key.equals("shape")) {
				shape = value;
			} else if (key.equals("defaultUnit")) {
				defaultUnit = value;
			} else if (key.equals("supplementaryMsg")) {
				supplementaryMsg = value;
			} else if (key.equals("surfaceCoatingTechnology")) {
				surfaceCoatingTechnology = value;
			} else if (key.equals("steelmakingProcess")) {
				steelmakingProcess = value;
			} else if (key.equals("shapeTechnology")) {
				shapeTechnology = value;
			} else if (key.equals("polishing")) {
				polishing = value;
			} else if (key.equals("hole")) {
				hole = value;
			}else if (key.equals("weightValue")) {
				weightValue = value;
			}
		}
		String param222 = request.getParameter("AVLNumber");
		System.out.println("======param =========" + param222);
		response.setContentType("application/json; charset=UTF-8");
		// 逻辑处理

		PartNumber = CreatePartClassificationNode.createPartByAVL(AVLPartNumber, parentNumber, count, RFQNumber, firstClassification, length,
				width, lwRatio, materialThickness, layerNumber, purpose, shape, defaultUnit, supplementaryMsg,
				surfaceCoatingTechnology, steelmakingProcess, shapeTechnology, polishing, hole, weightValue);
		System.out.println("PartNumber====>>>" + PartNumber);
		out.print(PartNumber);
	}

	/**
	 * 获取物料信息根据分类属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWTPart", method = { RequestMethod.POST, RequestMethod.GET })
	public void getWTPart(@RequestBody String data, HttpServletResponse response) throws Exception {
		System.out.println("===========after data0:" + data);
		response.setContentType("application/json; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		String result = "";
		String nodeName ="";
		System.out.println("===========after data1:" + data);
		// 请求数据已encode 需要decode
		data = URLDecoder.decode(data);
		PrintWriter out = response.getWriter();
		System.out.println("===========after data2:" + data);
		Map<String, String> map = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(data)) {
			JSONObject obj = new JSONObject(data);
			Iterator<String> it = obj.keys();
			while (it.hasNext()) {
				// 获得key
				String ibaName = it.next();
				String iabValue = obj.getString(ibaName);
				if("分类名称".equalsIgnoreCase(ibaName)) {
					nodeName=iabValue;
				}else {
					map.put(ibaName, iabValue);
				}
			}
		}
		result = ReadClassification.getWTPart(map,nodeName);
		// result = ob.toString();
		out.print(result);
	}

	/**
	 * 获取物料信息获取部件URL
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWTPartUrlByNumber", method = { RequestMethod.POST, RequestMethod.GET })
	public void getWTPartUrlByNumber(@RequestBody String data, HttpServletResponse response) throws Exception {
		String PartUrl = "";
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String partNumber = request.getParameter("partNumber");
		System.out.println("======param =========" + partNumber);

		String result = "";
		// 请求数据已encode 需要decode
		//data = URLDecoder.decode(data);
		data = new String(data.getBytes("iso8859-1"), "utf-8");
		System.out.println("===========after data:" + data);
		JSONObject obj = new JSONObject(data);
		Iterator<String> it = obj.keys();
		while (it.hasNext()) {
			System.out.println("====>>>start into ===>>");
			// 获得key
			String ibaName = it.next();
			String iabValue = obj.getString(ibaName);
			System.out.println("====>>>start 2 into ===>>" + iabValue);
			PartUrl = CreatePartClassificationNode.getPartUrlByNumber(iabValue);
		}

		// result = ob.toString();
		out.print(PartUrl);
	}

	/**
	 * 获取物料查询当前BOM结构
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchPartByAVLChange", method = { RequestMethod.POST, RequestMethod.GET })
	public void searchPartByAVLChange(@RequestBody String data, HttpServletResponse response) throws Exception {
		String PartUrl = "";
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String partNumber = request.getParameter("RFQNumber");
		System.out.println("======param =========" + partNumber);
//		VProductJurisdictionProcessor.getAuditRecord("GaoXin高鑫");
//		 ext.tianma.part.util.ExportPartByIBAValue.updatePartClassifiction(); 
//		countPartAndDocument.ExportExportPartByIBAValues();
		ext.tianma.part.util.ExportPartByIBAValue.ExportExportPartByIBAValues();
//		countPartAndDocument.ExportExportPartByIBAValuesC4();
//		countPartAndDocument.ExportExportPartByIBAValuesCF();
//		countPartAndDocument.ExportExportPartByIBAValuesXP();
//		countPartAndDocument.ExportExportPartByIBAValuesSP();
//		countPartAndDocument.ExportExportPartByIBAValuesRA();
//		countPartAndDocument.ExportExportPartByIBAValuesTOP();
//		countPartAndDocument.ExportExportPartByIBAValuesBOP();
//		countPartAndDocument.ExportDocumentByIBAValues();
//		CreatePartClassificationNode.getBOMIbaValuesForExcel("11");
		
		// 验证设置分类

		String result = "";
		// 请求数据已encode 需要decode
		data = URLDecoder.decode(data);
		System.out.println("===========after data:" + data);
		Map<String, String> map = new HashMap<String, String>();
		String parentNumber = "";
		String RFQNumber = "";
		if (StringUtils.isNotEmpty(data)) {
			JSONObject obj = new JSONObject(data);
			Iterator<String> it = obj.keys();
			while (it.hasNext()) {
				// 获得key
				String ibaName = it.next();
				String iabValue = obj.getString(ibaName);
				map.put(ibaName, iabValue);
			}
		}

		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			String value = map.get(key).toString();
			if (map.get(key) != null) {
				value = map.get(key).toString();
			}
			System.out.println("key==>>" + key + "==value=>>" + value);
			if (key.equals("childNumber")) {
				RFQNumber = value;
			} else if (key.equals("parentNumber")) {
				parentNumber = value;
			}
		}
		PartUrl = PartClassificationChange.searchPartByRFQ(RFQNumber, parentNumber);
		// JSONObject obj = new JSONObject(data);
		// Iterator<String> it = obj.keys();

		// while(it.hasNext()){
		// System.out.println("====>>>start into ===>>");
		// // 获得key
		// String ibaName = it.next();
		// String iabValue = obj.getString(ibaName);
		// System.out.println("====>>>start 2 into ===>>"+iabValue);
		// PartUrl = PartClassificationChange.searchPartByRFQ(iabValue);
		//
		// }

		// result = ob.toString();
		out.print(PartUrl);
	}

	/**
	 * 根据子，父物料删除link关系
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteUserLink", method = { RequestMethod.POST, RequestMethod.GET })
	public void deleteUserLink(@RequestBody String data, HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();

		String childNumber = "";
		String msg = "";
		// 请求数据已encode 需要decode
		data = URLDecoder.decode(data);
		String parentNumber = "";
		System.out.println("===========AVL data:" + data);
		Map<String, String> map = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(data)) {
			JSONObject obj = new JSONObject(data);
			Iterator<String> it = obj.keys();
			while (it.hasNext()) {
				// 获得key
				String ibaName = it.next();
				String iabValue = obj.getString(ibaName);
				map.put(ibaName, iabValue);
			}
		}

		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			String value = map.get(key).toString();
			if (map.get(key) != null) {
				value = map.get(key).toString();
			}
			System.out.println("key==>>" + key + "==value=>>" + value);
			if (key.equals("childNumber")) {
				childNumber = value;
			} else if (key.equals("parentNumber")) {
				parentNumber = value;
			}
		}
		// 逻辑处理

		msg = CreatePartClassificationNode.deletePartLink(childNumber, parentNumber);
		System.out.println("PartNumber====>>>" + msg);
		out.print(msg);
	}

	
	/**
	 * 获取物料BOM属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getClassifictionBomIba1", method = { RequestMethod.POST, RequestMethod.GET })
	public void getClassifictionBomIba1(@RequestBody String data, HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String classifiName = request.getParameter("classifictionName");
		String firstClassification = request.getParameter("firstClassification");
		System.out.println("======param =========" + classifiName);
		String result = "";
		// 请求数据已encode 需要decode
		data = URLDecoder.decode(data);
		System.out.println("===========after data:" + data);
		JSONObject obj = new JSONObject(data);
		Iterator<String> it = obj.keys();
		while (it.hasNext()) {
			System.out.println("====>>>start into ===>>");
			result = CreatePartClassificationNode.getBOMIbaValuesForExcel(classifiName,firstClassification);
		}

		// result = ob.toString();
		out.print(result);
	}
	
	
	/**
	 * 根据节点名称获取分类属性
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getClassifictionBomIba", method = { RequestMethod.POST, RequestMethod.GET })
	public void getClassifictionBomIba(
			@RequestParam(value = "classificationName", required = true) String classificationName,
			@RequestParam(value = "parentNumber", required = true) String parentNumber,
			@RequestParam(value = "firstClassification", required = true) String firstClassification, HttpServletResponse response)
			throws Exception {
		System.out.println("================<>>statr bom ==>>"+classificationName);
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String classificationName11 = request.getParameter("classificationName");
		String parentNumber11 = request.getParameter("parentNumber");
		System.out.println(
				"classificationName11===>>" + classificationName11 + "--classificationName--" + classificationName);
		System.out.println("parentNumber11===>>" + parentNumber11 + "--parentNumber--" + parentNumber);
		// System.out.println("======param =========" + param222);

		// 逻辑处理
		String result = "";
		result = CreatePartClassificationNode.getBOMIbaValuesForExcel(classificationName,firstClassification);

		out.print(result);
	}
	
	
}
